﻿using Newtonsoft.Json.Linq;
using Sunny.UI;
using System.Data;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace CoreShop.App.Controls
{
    public partial class UIKeyboardXp : UIUserControl
    {
        public UIKeyboardXp()
        {
            InitializeComponent();
        }
        public UIEdit edit;
        public const int WM_CHAR = 256;

        public void SetTextBox(UIEdit box)
        {
            edit = box;
        }

        private void btnCaps_Click(object sender, System.EventArgs e)
        {
            btnCaps.Selected = !btnCaps.Selected;
            foreach (var item in this.GetControls<UIButton>())
            {
                if (item.Name.Length == 1)
                    item.Text = btnCaps.Selected ? item.Text.ToUpper() : item.Text.ToLower();
            }

            SetState(VirtualKeys.VK_CAPITAL, btnCaps.Selected);
            //Close();
        }

        [DllImport("user32.dll")]
        static extern short GetKeyState(int nVirtKey);
        [DllImport("user32.dll")]
        static extern void keybd_event(byte bVk, byte bScan, uint dwFlags, uint dwExtraInfo);

        public static bool GetState(VirtualKeys Key)
        {
            return (GetKeyState((int)Key) == 1);
        }

        const uint KEYEVENTF_EXTENDEDKEY = 0x1;
        const uint KEYEVENTF_KEYUP = 0x2;

        public static void SetState(VirtualKeys Key, bool State)
        {
            if (State != GetState(Key))
            {
                keybd_event((byte)Key, 0x45, KEYEVENTF_EXTENDEDKEY | 0, 0);
                keybd_event((byte)Key, 0x45, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);
            }
        }

        public enum VirtualKeys : byte
        {
            VK_NUMLOCK = 0x90, //数字锁定键
            VK_SCROLL = 0x91,  //滚动锁定
            VK_CAPITAL = 0x14, //大小写锁定
            VK_A = 62
        }
        public delegate void OnOKButtonClick(object sender, string value);

        public event OnOKButtonClick OKButtonClick;

        private void UIKeyboard_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
        {
            //foreach (Control item in this.Controls)
            //{
            //    Rectangle rect = item.Bounds;
            //    rect.Offset(1, 1);
            //    e.Graphics.DrawRoundRectangle(Color.Gray, rect, 9);
            //}
        }

        private void btnOK_Click(object sender, System.EventArgs e)
        {
            UIButton btn = (UIButton)sender;
            OKButtonClick?.Invoke(btn, edit.Text);
            //DoValueChanged(this, btn.Tag.ToString().ToInt());
        }

        private void btnBackspace_Click(object sender, System.EventArgs e)
        {
            UIButton btn = (UIButton)sender;
            int value = btn.Tag.ToString().ToInt();
            if (edit == null) 
            {
                return;
            }
            if (btn.Name.Length == 1)
            {
                value = btn.Tag.ToString().ToInt() - 32;
            }
            int start = edit.SelectionStart;
            switch ((int)value)
            {
                case 1:
                    edit.Clear();
                    break;
                case 95:
                    edit.Text = edit.Text.Left(start) + "-" + edit.Text.Middle(start, edit.Text.Length);
                    start += 1;
                    edit.SelectionStart = start;
                    edit.Select(start, 0);
                    break;
                case 46:
                    edit.Text = edit.Text.Left(start) + "." + edit.Text.Middle(start, edit.Text.Length);
                    start += 1;
                    edit.SelectionStart = start;
                    edit.Select(start, 0);
                    break;
                case 13:
                    //OkButtonClick?.Invoke(this, Text);
                    break;
                default:
                    Sunny.UI.Win32.User.PostMessage(edit.Handle, WM_CHAR, (int)value, 0);
                    edit.SelectionStart = start;
                    edit.Select(start, 0);
                    break;
            }
        }

        public void SetCaps(bool caps)
        {
            btnCaps.Selected = caps;
            foreach (var item in this.GetControls<UIButton>())
            {
                if (item.Name.Length == 1)
                    item.Text = btnCaps.Selected ? item.Text.ToUpper() : item.Text.ToLower();
            }
        }

        private void timer1_Tick(object sender, System.EventArgs e)
        {
            bool CapsState = GetState(VirtualKeys.VK_CAPITAL);
            if (CapsState != btnCaps.Selected)
            {
                SetCaps(CapsState);
            }
        }

        private void UIKeyboardItem_VisibleChanged(object sender, System.EventArgs e)
        {
            timer1.Enabled = Visible;
        }

        public override void SetStyleColor(UIBaseStyle uiColor)
        {
            base.SetStyleColor(uiColor);
            BackColor = FillColor = RectColor = Color.FromArgb(238, 238, 238);
        }

    }
}
